﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using hlavny;

namespace modul3
{
    public partial class TestovaciFormular : Form
    {
        private cSysIndex indexy = new cSysIndex();
        private cSysConstr constraints = new cSysConstr();
        private Object[] lstIndexyDataArray;
        private Object[] lstConstraintsDataArray;
        private Dictionary<int, string> typyIndexov = new Dictionary<int, string>();

        public TestovaciFormular()
        {
            InitializeComponent();
            lstIndexy.DataSource = lstIndexyDataArray;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            List<int> vstupStlpce = new List<int>();
            for (int i = 0; i <= (clbStlpce.Items.Count - 1); i++)
            {
                if (clbStlpce.GetItemChecked(i))
                {
                    vstupStlpce.Add(int.Parse(clbStlpce.Items[i].ToString()));
                }
            }

            kkIndexType vstupTyp = (kkIndexType)((cComboBoxItem)comboType.SelectedItem).oValue;
            kkRetValue vlozene = indexy.createIdx(txtMeno.Text, vstupTyp, chbUnique.Checked, vstupStlpce);

            switch (vlozene)
            {
                case kkRetValue.OK:
                    break;
                case kkRetValue.ZiadenStlpec:
                    MessageBox.Show("Nezvolili ste ziaden stlpec", "", MessageBoxButtons.OK);
                    break;
                case kkRetValue.Neunikatne:
                    MessageBox.Show("Neunikatny index s poziadavkou na unikatnost", "", MessageBoxButtons.OK);
                    break;
                case kkRetValue.Chyba:
                    MessageBox.Show("Cudna chyba", "", MessageBoxButtons.OK);
                    break;
                default:
                    MessageBox.Show("Neznama chyba", "", MessageBoxButtons.OK);
                    break;

            }
            lstIndexyDataArray = indexy.aIndexy.ToArray();
            lstIndexy.DataSource = lstIndexyDataArray;
        }

        private void test_Load(object sender, EventArgs e)
        {
            comboType.Items.Add(new cComboBoxItem(kkIndexType.Bitmap, "Bitmap"));
            comboType.Items.Add(new cComboBoxItem(kkIndexType.Dense, "Dense"));
            comboType.Items.Add(new cComboBoxItem(kkIndexType.Hash, "Hash"));
            comboType.Items.Add(new cComboBoxItem(kkIndexType.Sparse, "Sparse"));
            comboType.DisplayMember = "oDisplay";
            comboType.ValueMember = "oValue";
            comboType.SelectedIndex = 0;

            comboConstraintType.Items.Add(new cComboBoxItem(kkConstraintType.PK, "PK"));
            comboConstraintType.Items.Add(new cComboBoxItem(kkConstraintType.FK, "FK"));
            comboType.DisplayMember = "oDisplay";
            comboType.ValueMember = "oValue";
            comboType.SelectedIndex = 0;

            for (int i = 1; i <= 10; i++)
            {
                comboTabulky.Items.Add(i);
            }
            comboTabulky.SelectedIndex = 0;
        }

        private void btnDrop_Click(object sender, EventArgs e)
        {
            int vstupIndexId = 0;
            try
            {
                vstupIndexId = int.Parse(txtColumnId.Text);
            }
            catch (Exception)
            {
            }

            kkRetValue vlozene = indexy.dropIdx(vstupIndexId);

            switch (vlozene)
            {
                case kkRetValue.OK:
                    break;
                case kkRetValue.Nenajdeny:
                    MessageBox.Show("Nenajdeny", "", MessageBoxButtons.OK);
                    break;
                default:
                    MessageBox.Show("Neznama chyba", "", MessageBoxButtons.OK);
                    break;

            }
            lstIndexyDataArray = indexy.aIndexy.ToArray();
            lstIndexy.DataSource = lstIndexyDataArray;
        }

        private void btnIsIndex_Click(object sender, EventArgs e)
        {
            int vstupIndexId = 0;
            try
            {
                vstupIndexId = int.Parse(txtColumnId.Text);
            }
            catch (Exception)
            {
            }
            int vystup = indexy.isInIdx(vstupIndexId);
            MessageBox.Show(vystup.ToString(), "", MessageBoxButtons.OK);
        }

        private void btnListIndex_Click(object sender, EventArgs e)
        {
            int vstupIndexId = 0;
            try
            {
                vstupIndexId = int.Parse(txtColumnId.Text);
            }
            catch (Exception)
            {
            }
            List<int> list = indexy.listIdx(vstupIndexId);
            string message = "";
            foreach (int indexId in list)
            {
                message += indexId.ToString() + " ";
            }
            MessageBox.Show(message, "", MessageBoxButtons.OK);
        }

        private void btnCreateConstraint_Click(object sender, EventArgs e)
        {
            List<int> stlpceZdrojove = new List<int>();
            List<int> stlpceReferencovane = new List<int>();
            for (int i = 0; i <= (clbStlpce.Items.Count - 1); i++)
            {
                if (clbZdrojoveStlpce.GetItemChecked(i))
                {
                    stlpceZdrojove.Add(int.Parse(clbZdrojoveStlpce.Items[i].ToString()));
                }
            }

            for (int i = 0; i <= (clbStlpce.Items.Count - 1); i++)
            {
                if (clbReferencovaneStlpce.GetItemChecked(i))
                {
                    stlpceReferencovane.Add(int.Parse(clbReferencovaneStlpce.Items[i].ToString()));
                }
            }

            kkConstraintType vstupTyp = (kkConstraintType)((cComboBoxItem)comboConstraintType.SelectedItem).oValue;
            kkRetValue vlozene = constraints.CreateCon(txtMeno.Text, vstupTyp, stlpceZdrojove, stlpceReferencovane);

            switch (vlozene)
            {
                case kkRetValue.OK:
                    break;
                case kkRetValue.ZiadenStlpec:
                    MessageBox.Show("Nezvolili ste ziaden stlpec", "", MessageBoxButtons.OK);
                    break;
                case kkRetValue.Neunikatne:
                    MessageBox.Show("Neunikatny index s poziadavkou na unikatnost", "", MessageBoxButtons.OK);
                    break;
                case kkRetValue.Chyba:
                    MessageBox.Show("Cudna chyba", "", MessageBoxButtons.OK);
                    break;
                default:
                    MessageBox.Show("Neznama chyba", "", MessageBoxButtons.OK);
                    break;

            }
            lstConstraintsDataArray = indexy.aIndexy.ToArray();
            lstConstraints.DataSource = lstIndexyDataArray;
        }
    }
}
